package com.fuyu.system.controller;


import com.fuyu.common.model.R;
import com.fuyu.common.web.controller.BaseController;
import com.fuyu.system.models.form.SysMenuForm;
import com.fuyu.system.models.search.SysMenuSearch;
import com.fuyu.system.models.vo.menu.SysMenuListTreeVO;
import com.fuyu.system.models.vo.menu.SysMenuRouterVO;
import com.fuyu.system.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 菜单权限表(SysMenu)表控制层
 *
 * @author makejava
 * @since 2024-04-07 17:49:35
 */
@Tag(name = "菜单权限表", description = "菜单权限表")
@RestController
@RequestMapping("/sys/sysMenu")
public class SysMenuController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 获取路由菜单tree
     */
    @Operation(summary = "获取路由菜单tree")
    @GetMapping("/router")
    public R<List<SysMenuRouterVO>> getRouterTree() {

        return success(this.sysMenuService.getTree(getUserId()));
    }


    /**
     * 查询所有菜单tree数据
     */
    @Operation(summary = "查询所有菜单tree数据")
    @GetMapping("/tree")
    public R<List<SysMenuListTreeVO>> selectMenuTree(SysMenuSearch params) {
        return success(this.sysMenuService.getMenuPageList(params));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条数据")
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.sysMenuService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sysMenu 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增数据")
    @PostMapping
    public R<Boolean> insert(@RequestBody @Valid SysMenuForm sysMenu) {
        return crud(this.sysMenuService.save(SysMenuForm.toEntity(sysMenu)));
    }

    /**
     * 修改数据
     *
     * @param sysMenu 实体对象
     * @return 修改结果
     */
    @PutMapping
    @Operation(summary = "修改数据")
    public R<Boolean> update(@RequestBody @Valid SysMenuForm sysMenu) {
        return crud(sysMenuService.updateById(SysMenuForm.toEntity(sysMenu)));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @Operation(summary = "删除数据")
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return success(this.sysMenuService.removeByIds(idList));
    }
}

